﻿// ***********************************************************************
// Assembly         : YERIL_PI_INT
// Author           : Diego Bonilla
// Created          : 03-02-2014
//
// Last Modified By : Diego Bonilla
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="Materiales.aspx.cs" company="Development Solutions CR">
//     Copyright (c) Development Solutions CR. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// The Login namespace.
/// </summary>
namespace YERIL_PI_INT.PaginasWebs.Modulos.Login
{
    /// <summary>
    /// Class Materiales.
    /// </summary>
    public partial class Materiales : System.Web.UI.Page
    {   static string  FormAccion = "";
    /// <summary>
    /// The mensaje error
    /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The codigo
        /// </summary>
        static int codigo = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["FormAccion"] != null)
                {
                    FormAccion = Session["FormAccion"].ToString();

                }


                if (Session["Codigo"] != null)
                {
                    codigo = Convert.ToInt32(Session["Codigo"]);

                }


                if (!IsPostBack)
                {
                    Cargar_Combos();
                    dlMedida.SelectedValue = "1";
                    
                    if (FormAccion == "Editar")
                    {
                        if (codigo != 0)
                        {
                            CargarEditar();
                        }
                    }

                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        /// <summary>
        /// Cargars the editar.
        /// </summary>
        private void CargarEditar()
        {
            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            WSGestion.MOD_Material objMaterial = new WSGestion.MOD_Material();
            objMaterial = wsgest.SEL_MaterialByID(codigo);
            txtDescripcion.Text = objMaterial.MAT_descripcion;
            txtDimensioAlto.Text = objMaterial.MAT_dimalto.ToString();
            txtDimensioAncho.Text = objMaterial.MAT_dimancho.ToString();
            txtDimensioGrosor.Text = objMaterial.MAT_dimgrosor.ToString();
            txtDimensioLargo.Text = objMaterial.MAT_dimlargo.ToString();
            txtPesoKG.Text = objMaterial.MAT_pesokg.ToString();
            txtPesoLB.Text = objMaterial.MAT_pesolb.ToString();
            txtPrecioMat.Text = objMaterial.MAT_preciomat.ToString();
            txtPrecioMatIVI.Text = objMaterial.MAT_precioivimat.ToString();
            dlMedida.SelectedValue = objMaterial.MAT_codmedida.ToString();
        }

        /// <summary>
        /// Cargar_s the combos.
        /// </summary>
        private void Cargar_Combos()
        {
            try
            {
                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                List<WSGestion.MOD_Unidad_Medida> ListaUnidades = wsgest.SEL_Grid_Unidad_Medida().ToList();
                this.dlMedida.DataSource = ListaUnidades;
                this.dlMedida.DataValueField = "UNI_CODMEDIDA";
                this.dlMedida.DataTextField = "UNI_NOMENCLATURA";
                this.dlMedida.DataBind();

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }


        }

        /// <summary>
        /// Handles the Click event of the btnSalir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("GridMateriales.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnAgregarCliente control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregarCliente_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (FormAccion == "Agregar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Material objMaterial = new WSGestion.MOD_Material();
                    objMaterial.MAT_descripcion = txtDescripcion.Text;
                    objMaterial.MAT_dimalto = Convert.ToDecimal(txtDimensioAlto.Text);
                    objMaterial.MAT_dimancho = Convert.ToDecimal(txtDimensioAncho.Text);
                    objMaterial.MAT_dimgrosor =  Convert.ToDecimal(txtDimensioGrosor.Text);
                    objMaterial.MAT_dimlargo = Convert.ToDecimal( txtDimensioLargo.Text);
                    objMaterial.MAT_pesokg =  Convert.ToDecimal(txtPesoKG.Text);
                    objMaterial.MAT_pesolb = Convert.ToDecimal( txtPesoLB.Text);
                    objMaterial.MAT_preciomat = Convert.ToDecimal( txtPrecioMat.Text);
                    objMaterial.MAT_precioivimat = Convert.ToDecimal(txtPrecioMatIVI.Text);
                    objMaterial.MAT_codmedida = Convert.ToInt32(dlMedida.SelectedValue.ToString());
                   
                    //int cant_mat = wsgest.sel(txtNit.Text);
                    //if (cant_mat <= 0)
                    //{
                    codigo =wsgest.CRE_Material(objMaterial);
                        if (codigo!=0)
                        {
                            Session["Codigo"] = codigo;
                            Response.Redirect("MaterialProveedor.aspx");
                        }
                        else
                        {
                            MensajeError = "Procedimiento Almacenado no aceptado";
                        }
                    ////}
                    //else
                    //{
                    //    MensajeError = "El Material " + txtNit.Text + " ya existe";
                    //}

                }
                if (FormAccion == "Editar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Material objMaterial = new WSGestion.MOD_Material();
                    objMaterial.MAT_codmaterial = codigo;
                    objMaterial.MAT_descripcion = txtDescripcion.Text;
                    objMaterial.MAT_dimalto = Convert.ToDecimal(txtDimensioAlto.Text);
                    objMaterial.MAT_dimancho = Convert.ToDecimal(txtDimensioAncho.Text);
                    objMaterial.MAT_dimgrosor = Convert.ToDecimal(txtDimensioGrosor.Text);
                    objMaterial.MAT_dimlargo = Convert.ToDecimal(txtDimensioLargo.Text);
                    objMaterial.MAT_pesokg = Convert.ToDecimal(txtPesoKG.Text);
                    objMaterial.MAT_pesolb = Convert.ToDecimal(txtPesoLB.Text);
                    objMaterial.MAT_preciomat = Convert.ToDecimal(txtPrecioMat.Text);
                    objMaterial.MAT_precioivimat = Convert.ToDecimal(txtPrecioMatIVI.Text);
                    objMaterial.MAT_codmedida = Convert.ToInt32(dlMedida.SelectedValue.ToString());

                    wsgest.UPD_Material(objMaterial);
                    Session["Codigo"] = codigo;
                    Response.Redirect("MaterialProveedor.aspx");

                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }
    }
}